package com.bihu.bihudemo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bihu.bihudemo.config.MqConfig;
import com.bihu.bihudemo.entity.User;
import com.bihu.bihudemo.exception.ErrorEnum;
import com.bihu.bihudemo.mq.PostMessage;
import com.bihu.bihudemo.result.Result;
import com.bihu.bihudemo.service.UserService;
import com.bihu.bihudemo.utils.BCryptPasswordEncoderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2020-07-15 17:08:49
 */
@RestController
@Api(tags = "用户管理的接口")
@RequestMapping("user")
public class UserController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @Resource
    BCryptPasswordEncoderUtil bCryptPasswordEncoderUtil;

    @Autowired
    RabbitTemplate rabbitTemplate;
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param user 查询实体
     * @return 所有数据
     */
    @GetMapping
    @ApiOperation("选择所有用户的操作")
    public R selectAll(Page<User> page, User user) {
        return success(this.userService.page(page, new QueryWrapper<>(user)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.userService.getById(id));
    }



    @Cacheable(value="users", key="#uid")
    @ApiOperation("根据ID获取用户信息")
    @GetMapping("/getByUid")
    public  Result getByUserID(@RequestParam("uid")int uid){
        User user = userService.getById(uid);

        new LinkedList<>();
        return Result.ok().put("user",user);
    }
    /**
     * 新增数据
     *
     * @param user 实体对象
     * @return 新增结果
     */
    @ApiOperation("新增用户接口")
    @PostMapping
    public Result insert(@RequestBody User user) {
        User userExist = userService.getUserByUserName(user.getUsername());
        if(userExist!=null){
            return Result.error(ErrorEnum.DUPLICATE_KEY);
        }else {
            //注册的时候记得BCY加密密码，要不然框架不认明文
            String encodepwd = bCryptPasswordEncoderUtil.encode(user.getPassword());
            user.setPassword(encodepwd);
            if(userService.save(user)){
                PostMessage postMessage = new PostMessage();
                postMessage.setUser(user);
                rabbitTemplate.convertAndSend("bihu",MqConfig.EmailKEY,postMessage);
                return Result.ok().put("RegisterMsg","注册成功");
            }else {
                return Result.error(ErrorEnum.UNKNOWN);
            }
        }
    }

    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody User user) {
        return success(this.userService.updateById(user));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.userService.removeByIds(idList));
    }
}